package com.yycx.module.bus.provider.service.hy.impl;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.hy.BusPaymentDetails;
import com.yycx.module.bus.provider.mapper.hy.BusPaymentDetailsMapper;
import com.yycx.module.bus.provider.service.hy.BusPaymentDetailsService;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.file.provider.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.query.CriteriaQuery;
import org.springframework.transaction.annotation.Propagation;
import com.yycx.common.mybatis.query.CriteriaSave;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 付款通知单明细接口实现类
 *
 * @author flyme
 * @date 2023-12-05
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusPaymentDetailsServiceImpl extends BaseServiceImpl<BusPaymentDetailsMapper, BusPaymentDetails> implements BusPaymentDetailsService {

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private BusSalemanService salemanService;

    @Autowired
    private SysFileService fileService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusPaymentDetails bpd, EntityMap extra) {
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusPaymentDetails> cq, BusPaymentDetails bpd, EntityMap requestMap) {
        cq.orderByDesc("bpd.createTime");
        return ResultBody.ok();
    }

    public BigDecimal totalSettleAmount(Long paymentId) {
        CriteriaQuery<BusPaymentDetails> cq = new CriteriaQuery(BusPaymentDetails.class);
        cq.select("sum(settleAmount)");
        cq.eq(true, "paymentId", paymentId);
        BigDecimal amount = getObj(cq, e -> new BigDecimal(e.toString()));
        if (FlymeUtils.isEmpty(amount)) {
            amount = new BigDecimal("0");
        }
        return amount;
    }

    @Override
    public ResultBody getAmountInfo(Long paymentId, Long paymentDetailsId) {
        BusPayment payment = paymentService.getById(paymentId);
        //总金额
        BigDecimal totalSettleAmount = payment.getAmount();
        //已分解金额
        BigDecimal settleAmount = totalSettleAmount(paymentId);
        BusPaymentDetails busPaymentDetails = new BusPaymentDetails();
        totalSettleAmount = totalSettleAmount.subtract(settleAmount);
        busPaymentDetails.setSettleAmount(totalSettleAmount);

        return ResultBody.ok(busPaymentDetails);
    }

    @Override
    public void afterGet(CriteriaQuery cq, EntityMap result) {
        Long paymentDetailsId = result.getLong("paymentDetailsId");
        Integer fileGroup = cq.getInt("fileGroup", null);
        if (FlymeUtils.isNotEmpty(paymentDetailsId)) {
            List<EntityMap> fileList = fileService.selectFileListByFileGroup(paymentDetailsId.toString(), "BusPaymentDetails", fileGroup);
            result.put("paymentImage", fileList);
        }
        super.afterGet(cq, result);
    }
}
